﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.Code;
using Telerik.Web.UI;
using Company.Merchandising.BusinessLayer.DataTransferObjects;

namespace WebApplication.Controls.Management.Company
{
	public partial class AddressEditControl : EntityUserControl
	{
		protected void Page_Init(object sender, EventArgs e)
		{
			ConfigureSelectCityForm();
			GridEditableItem item = this.Parent.NamingContainer as GridEditableItem;
			if (item == null || item.DataItem == null)
			{
				return;
			}

			if (item.DataItem is GridInsertionObject)
			{
				btUpdate.Visible = false;
				return;
			}

			btInsert.Visible = false;
			DistrictTextBox.Text = item["District"].Text;
			StreetTextBox.Text = item["Street"].Text;
			BuildingNumberTextBox.Text = item["BuildingNumber"].Text;
			LocalNumberTextBox.Text = item["LocalNumber"].Text;
			CityIdTextBox.Text = item["CityId"].Text;
			CityNameTextBox.Text = item["CityName"].Text;
			ProvinceNameTextBox.Text = item["ProvinceName"].Text;
			return;
		}

		public DTOAddress EditedAddress
		{
			get
			{
				DTOAddress dtoAddress = new DTOAddress()
				{
					Disctrict = DistrictTextBox.Text,
					Street = StreetTextBox.Text,
					BuildingNumber = BuildingNumberTextBox.Text,
					LocalNumber = LocalNumberTextBox.Text
				};

				if (string.IsNullOrEmpty(CityIdTextBox.Text))
				{
					return dtoAddress;
				}

				dtoAddress.City = new DTOCity()
				{
					Id = int.Parse(CityIdTextBox.Text),
					Name = CityNameTextBox.Text
				};

				return dtoAddress;
			}
		}

		private void ConfigureSelectCityForm()
		{
			RadWindow1.OpenerElementID = SelectCityButton.ClientID;
//            RadWindow1.OnClientClose = @"
//            function(sender, args)
//            {
//				var city = args.get_argument();
//				if (city) {
//					$get('" + CityIdTextBox.ClientID + @"').value = city.Id;
//					$get('" + CityNameTextBox.ClientID + @"').value = city.Name;
//					$get('" + ProvinceNameTextBox.ClientID + @"').value = city.Province.Name;
//				}
//            }
//            ";
		}
	}
}